global sdv	=  1.645

************************************************************************************************
****************************** Prepare Datasets ***************************************
************************************************************************************************
foreach data in bs_decomposition_SS_ProdR_new_kl bs_decomposition_SS_ProdR_new_kl_3lags bs_decomposition_SS_ProdQ_new_kl bs_decomposition_SS_CS_kl_nic4 bs_decomposition_SS_CS_kl_nic2_by_year ///
				bs_decomposition_SS_ProdQ_new_klme_TFPQIV bs_decomposition_SS_ProdQ_new_klme_TFPQIV_2lags bs_decomposition_SS_ProdQ_new_klme_TFPQIV_rshare bs_decomposition_SS_CS_klme_nic4 bs_decomposition_SS_CS_klme_nic2_by_year bs_decomposition_SS_ProdQ_new_klm {
use "$working/results/`data'", clear

preserve
	drop if bs == 0 | bs == -1
	drop *_F_* *_N2_* *_N3_*
	collapse (sd) *
	unab varl: *b2* *b3* 

	foreach var of local varl {
		rename `var' se`var'
	}
	replace bs = 0
	save "$working/temp/bstemp", replace
restore
preserve
	drop *_F_* *_N2_* *_N3_*
	keep if bs == -1
	unab varl: *b2* *b3*  
	foreach var of local varl {
		rename `var' se`var'
	}
	save "$working/temp/bstemp2", replace
restore

keep if bs == 0
merge 1:1 bs using "$working/temp/bstemp"
drop _merge 
keep *b2* *b3*   
drop *_F_*
append using "$working/temp/bstemp2"
unab varl: *b2* *b3*   
local mainvars: subinstr local varl "se" "", all
local mainvars_uniq : list uniq mainvars
foreach var of local mainvars_uniq {
    replace `var' = `var'[_n-1] if missing(`var')
}


*preserve
keep  *_b2* *_b3*
drop *_OI*  *RI* 
gen id = _n
ren (*_b2_y *_b2_yn *_b2_ycn) (y2* yn2* ycn2*)
ren (*_b3_y *_b3_yn *_b3_ycn) (y3* yn3* ycn3*)
ren (*_I*) (I*)
reshape long y2 yn2 ycn2 y2se yn2se ycn2se Iy2 Iyn2 Iycn2 Iy2se Iyn2se Iycn2se y3 yn3 ycn3 y3se yn3se ycn3se Iy3 Iyn3 Iycn3 Iy3se Iyn3se Iycn3se, i(id) j(variables) string
foreach var in y yn ycn Iy Iyn Iycn {
	gen lows_`var'2 = `var'2 - ($sdv * `var'2se)
	gen highs_`var'2 = `var'2 + ($sdv * `var'2se)
	gen lows_`var'3 = `var'3 - ($sdv * `var'3se)
	gen highs_`var'3 = `var'3 + ($sdv * `var'3se)
}

		replace variables = "R. TFP" if variables == "diff_tfpr"
		gen ordering = 1 if variables == "R. TFP"
		replace variables = "M. Demand" if variables == "dif_d_bar"
		replace ordering = 2 if variables == "M. Demand"
		replace variables = "M. TFPQ" if variables == "dif_a_bar"
		replace ordering = 3 if variables == "M. TFPQ"
		replace variables = "Covariance" if variables == "dif_cov_stay"
		replace ordering = 4 if variables == "Covariance"
		replace variables = "Scope" if variables == "dif_var_adj"
		replace ordering = 5 if variables == "Scope"
		replace variables = "Adding" if variables == "adding_term"
		replace ordering = 6 if variables == "Adding"
		replace variables = "Dropping" if variables ==  "drop_term"
		replace ordering = 7 if variables == "Dropping"
		sencode variables, gen(vars) gsort(ordering)
	gen dataset = "`data'"
	save "$working/temp/robustness_`data'", replace
	}
************************************************************************************************
****************************** KL Figures ***************************************
************************************************************************************************

use "$working/temp/robustness_bs_decomposition_SS_ProdR_new_kl", clear
append using "$working/temp/robustness_bs_decomposition_SS_ProdR_new_kl_3lags"
append using "$working/temp/robustness_bs_decomposition_SS_ProdQ_new_kl"
append using "$working/temp/robustness_bs_decomposition_SS_CS_kl_nic4"
append using "$working/temp/robustness_bs_decomposition_SS_CS_kl_nic2_by_year"
		
		gen vars1 = (vars-.3)*2
		gen vars2 = (vars-.15)*2
		gen vars3 = (vars)*2
		gen vars4 = (vars+.15)*2
		gen vars5 = (vars+.3)*2

	global sym O
	global cl blue
foreach fig in y2 yn2 ycn2 {
	global p `fig'
	do "$code/estimation/postest/subfiles/decomposition_figures_robustness.do"
}

	global sym D
	global cl blue
foreach fig in y3 yn3 ycn3 {
	global p `fig'
	do "$code/estimation/postest/subfiles/decomposition_figures_robustness.do"
}

	global sym S
	global cl red
foreach fig in Iy2 Iyn2 {
	global p `fig'
	do "$code/estimation/postest/subfiles/decomposition_figures_robustness.do"
}

************************************************************************************************
******************************* KLME Figures *****************************************************
************************************************************************************************
use "$working/temp/robustness_bs_decomposition_SS_ProdQ_new_klme_TFPQIV", clear
append using "$working/temp/robustness_bs_decomposition_SS_ProdQ_new_klme_TFPQIV_2lags" 
append using "$working/temp/robustness_bs_decomposition_SS_ProdQ_new_klme_TFPQIV_rshare"
append using "$working/temp/robustness_bs_decomposition_SS_CS_klme_nic4"
append using "$working/temp/robustness_bs_decomposition_SS_CS_klme_nic2_by_year" 
append using "$working/temp/robustness_bs_decomposition_SS_ProdQ_new_klm"


		gen vars1 = (vars-.3)*2
		gen vars2 = (vars-.15)*2
		gen vars3 = (vars)*2
		gen vars4 = (vars+.15)*2
		gen vars5 = (vars+.3)*2
		gen vars6 = (vars+.45)*2

	global sym O
	global cl blue
foreach fig in y2 yn2 ycn2 {
	global p `fig'
	do "$code/estimation/postest/subfiles/decomposition_figures_robustness_GO.do"
}

	global sym D
	global cl blue
foreach fig in y3 yn3 ycn3 {
	global p `fig'
	do "$code/estimation/postest/subfiles/decomposition_figures_robustness_GO.do"
}

	global sym S
	global cl red
foreach fig in Iy2 Iyn2 {
	global p `fig'
	do "$code/estimation/postest/subfiles/decomposition_figures_robustness_GO.do"
}		
